<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layout}">

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

    <title th:text="#{cas.mfa.u2f.pagetitle}">U2f Login View</title>
    <link href="../../static/css/cas.css" rel="stylesheet" th:remove="tag" />
    <script th:src="@{/js/u2f/u2f-api.js}"></script>

    <script th:inline="javascript">

        /*<![CDATA[*/

        var appId =  /*[[${u2fAuth.appId}]]*/;

        var version =  /*[[${u2fAuth.version}]]*/;

        var challenge =  /*[[${u2fAuth.challenge}]]*/;

        var keyHandle =  /*[[${u2fAuth.keyHandle}]]*/;

        setTimeout(function () {
            var registeredKeys = [{ version: version, keyHandle: keyHandle }];

            u2f.sign(appId, challenge, registeredKeys, function (data) {
                var form = document.getElementById('form');
                var reg = document.getElementById('tokenResponse');
                reg.value = JSON.stringify(data);
                form.submit();
            });
        }, 1000);

        /*]]>*/

    </script>

</head>

<body>
    <main role="main" class="container mt-3 mb-3">
        <div layout:fragment="content" id="login" class="mdc-card mdc-card-content w-lg-50 p-4 m-auto">
            <h3 th:text="#{cas.mfa.u2f.authentication.device}">Authenticate Device</h3>
            <div th:utext="#{cas.mfa.u2f.authentication.message}" th:remove="tag">
                <p><strong>Please touch the flashing U2F device now.</strong></p>
                <p> You may be prompted to allow the site permission to access your security keys. After granting
                    permission, the device will start to blink.</p>
            </div>

            <form method="POST" id="form" th:object="${credential}" th:action="@{/login}">
                <input type="hidden" name="tokenResponse" id="tokenResponse" th:field="*{token}" />
                <input type="hidden" name="_eventId" value="submit" />
                <input type="hidden" name="execution" th:value="${flowExecutionKey}" />
                <input type="hidden" name="geolocation" />
            </form>
        </div>
    </main>
</body>

</html>